<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$cache_time=2;
$OJ_CACHE_SHARE=false;
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once("./include/my_func.inc.php");
require_once("./classes/const/ResultID.php");
require_once("./config/privliege_config.php");

$view_title= "$MSG_STATUS";

// 按设置的语言启动
require_once("./lang/cn.php");
require_once("./include/const.inc.php");

// 判断用户是否已登录
if (!isset($_SESSION['user_id'])) {
    $view_errors= "<a href=loginpage.php>$MSG_Login</a>";
    require("template/".$OJ_TEMPLATE."/error.php");
    exit(0);
}

$start_first=true;
$order_str=" ORDER BY `solution_id` DESC ";

// 查看提交统计
if (isset($_GET['top'])){
    $top=strval(intval($_GET['top']));
    if ($top!=-1) {
        $sql=$sql."AND `solution_id`<='".$top."' ";
    }
}

// 查看指定问题的提交状态
$problem_id="";
if (isset($_GET['problem_id'])&&$_GET['problem_id']!=""){
    $problem_id=strval(intval($_GET['problem_id']));
    if ($problem_id!='0'){
        $sql=$sql."AND `problem_id`='".$problem_id."' ";
        $str2=$str2."&problem_id=".$problem_id;
	}
	else {
        $problem_id="";
    }
}

// 查看指定用户的提交状态
$user_id="";
if (isset($_GET['user_id'])){
        $user_id=trim($_GET['user_id']);
        if (is_valid_user_name($user_id) && $user_id!=""){
			if($OJ_MEMCACHE){
                $sql=$sql."AND `user_id`='".addslashes($user_id) ."' ";
			}else{
                $sql=$sql."AND `user_id`=? ";
			}
                if ($str2!="") $str2=$str2."&";
                $str2=$str2."user_id=".urlencode($user_id);
        }else $user_id="";
}

// 查看指定语言的提交状态
if (isset($_GET['language'])) {
    $language=intval($_GET['language']);
}
else {
    $language=-1;
}

if ($language>count($language_ext) || $language<0) $language=-1;
if ($language!=-1){
        $sql=$sql."AND `language`='".($language)."' ";
        $str2=$str2."&language=".$language;
}
if (isset($_GET['jresult'])) $result=intval($_GET['jresult']);
else $result=-1;

if ($result>12 || $result<0) $result=-1;
if ($result!=-1&&!$lock){
        $sql=$sql."AND `result`='".($result)."' ";
        $str2=$str2."&jresult=".$result;
}



if($OJ_SIM){
       // $old=$sql;
        $sql="select * from solution solution left join `sim` sim on solution.solution_id=sim.s_id ".$sql;
        if(isset($_GET['showsim'])&&intval($_GET['showsim'])>0){
                $showsim=intval($_GET['showsim']);
        	$sql.=" and sim.sim>=$showsim";
                $str2.="&showsim=$showsim";
        }
	
        //$sql=$sql.$order_str." LIMIT 20";
}else{
 	$sql="select * from `solution` ".$sql;
}

$sql=$sql.$order_str." LIMIT 20";
echo $sql;

if($OJ_MEMCACHE){
	require("./include/memcache.php");
	$result = mysql_query_cache($sql);
	if($result) $rows_cnt=count($result);
	else $rows_cnt=0;
}else{
	if (isset($_GET['user_id'])){
		$result = pdo_query($sql,$user_id);
	}else{
		$result = pdo_query($sql);
	}
	
	if($result) $rows_cnt=count($result);
	else $rows_cnt=0;
}
$top=$bottom=-1;
$cnt=0;
if ($start_first){
        $row_start=0;
        $row_add=1;
}else{
        $row_start=$rows_cnt-1;
        $row_add=-1;
}

$view_status = Array();
$last=0;
for ($i=0; $i < $rows_cnt; $i++)
{
    $row=$result[$i];
    if($i==0 && $row['result']<4 ) {
        $last=$row['solution_id'];
    }
    if ($top==-1) {
        $top=$row['solution_id'];
    }
    $bottom=$row['solution_id'];
    $cnt=1-$cnt;
    $view_status[$i][0]=$row['solution_id'];
    if ($row['contest_id']>0)
    {
        $view_status[$i][1]= "<a href='contestrank.php?cid=".$row['contest_id']."&user_id=".$row['user_id']."#".$row['user_id']."'>".$row['user_id']."</a>";
    }
    else {
        $view_status[$i][1]= "<a href='userinfo.php?user=".$row['user_id']."'>".$row['user_id']."</a>";
    }

    // 姓名
    $view_status[$i][2]=$row['user_name'];

    if ($row['contest_id']>0) {
        $view_status[$i][3]= "<div class=center><a href='problem.php?cid=".$row['contest_id']."&pid=".$row['num']."'>";
        if(isset($cid)){
            $view_status[$i][3].= $PID[$row['num']];
        }
        else{
            $view_status[$i][3].= $row['problem_id'];
        }
        $view_status[$i][3].="</div></a>";
    }
    else{
        $view_status[$i][3]= "<div class=center><a href='problem.php?id=".$row['problem_id']."'>".$row['problem_id']."</a></div>";
    }

    // 评测结果提示语和标签文字
    $resultID = intval($row['result']);
    $resultMsg = '';
    $resultTips = '';
	switch( $resultID ) {
        case ResultID::ID_AC:
            $resultTips = $MSG_HELP_AC;
            $resultMsg = '正确';
            break;
        case ResultID::ID_PE:
            $resultMsg = '格式错误';
            $resultTips = $MSG_HELP_PE;
			break;
		case ResultID::ID_WA:
            $resultMsg = '答案错误';
            $resultTips = $MSG_HELP_WA;
			break;
		case ResultID::ID_TLE:
            $resultTips = $MSG_HELP_TLE;
            $resultMsg = '时间超限';
			break;
		case ResultID::ID_MLE:
            $resultTips = $MSG_HELP_MLE;
            $resultMsg = '内存超限';
			break;
		case ResultID::ID_OLE:
            $resultTips = $MSG_HELP_OLE;
            $resultMsg = '输出超限';
			break;
		case ResultID::ID_RE:
            $resultTips = $MSG_HELP_RE;
            $resultMsg = '运行错误';
			break;
		case ResultID::ID_CE:
            $resultTips = $MSG_HELP_CE;
            $resultMsg = '编译错误';
			break;
        default:
            $resultMsg = '正在评测';
            break;
	}

	// 评测状态样式
    $judge_color=Array(
        "btn gray",
        "btn btn-info",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-success",
        "btn btn-danger",
        "btn btn-danger",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-info");
    $resultClass = $judge_color[$row['result']];

	// 展示正确率
	if ( isset($row['pass_rate']) )
    {
        // 需考虑浮点误差
        if ( $row['pass_rate'] > 0 && $row['pass_rate'] < 0.98 ) {
            $resultMsg .= (100-$row['pass_rate']*100)."%"; // 错误率
        }
        else if ( $row['pass_rate'] >= 0.98 ) {
            $resultMsg .= "100%";
        }
    }

	// 是否允许查看评测详情
    // 1. 用户已登录且当前登录用户为代码的提交用户
    $isAllowSeeDetail = isset($_SESSION['user_id']) && ($row['user_id']==$_SESSION['user_id']);
    // 2. 登录的用户拥有source_browser权限
    $isAllowSeeDetail |= isset($_SESSION['teacher']);
    // 3. 登录的用户是管理员
    $isAllowSeeDetail |= isset($_SESSION['administrator']);

    // 4. 登陆的账号是学生家长
    $isAllowSeeDetail |= isset($_SESSION['parent']);


    // 获取评测详情的链接
	$resultDetailUrl = '';

	// 编译错误
    if ( $resultID == ResultID::ID_CE && $isAllowSeeDetail ) {
        $resultDetailUrl =  "ceinfo.php?sid=".$row['solution_id'];
    }

    // 答案部分通过
    if ( $resultID != ResultID::ID_AC && $isAllowSeeDetail )
    {
        $resultDetailUrl = "reinfo.php?sid=".$row['solution_id'];
    }

    $view_status[$i][4] ="<span class='hidden' style='display:none' result='".$row['result']."' ></span>";
    // 答案正确，不添加跳转链接
    if ( $resultID == ResultID::ID_AC || strlen($resultDetailUrl) == 0 ) {
        $view_status[$i][4] .= "<a class='".$resultClass."'".">".$resultMsg."</a>";
    }
    else {
        $view_status[$i][4] .= "<a href='".$resultDetailUrl."'"." class='".$resultClass."'".">".$resultMsg."</a>";
    }

    // 展示内存占用和运行时间
    if ( $resultID >= ResultID::ID_AC ){
        // 评测完成
        $view_status[$i][5]= "<div id=center class=red>".$row['memory']."</div>";
        $view_status[$i][6]= "<div id=center class=red>".$row['time']."</div>";

        // 是否允许查看已提交的代码
        // 1. 用户已登录，并且当前用户是源码的提交用户
        $isAllowSeeCode = isset($_SESSION['user_id']) && strtolower($_SESSION['user_id']) == strtolower($row['user_id']);
        // 2. 用户已登录,且系统允许相互查看代码
        if ( ! $isAllowSeeCode ) {
            $isAllowSeeCode = isset($_SESSION['user_id']) && privliegeconfig::isAllowSeeCode();
        }
        // 3. 用户是管理员
        if ( ! $isAllowSeeCode ) {
            $isAllowSeeCode = isset($_SESSION['administrator']) && $_SESSION['administrator'];
        }
        // 4. 用户是source_browser
        if ( ! $isAllowSeeCode ) {
            $isAllowSeeCode = isset($_SESSION['teacher']) && $_SESSION['teacher'];
        }

        // 5. 登陆的账号是学生家长
        if ( ! $isAllowSeeCode ) {
            $isAllowSeeCode = isset($_SESSION['parent']) && $_SESSION['parent'];
        }

        // 是否运行编辑已提交的代码
        // 用户已登录，并且登录用户即为提交代码的用户
        $isAllowEditCode = isset($_SESSION['user_id']) && strtolower($row['user_id'])== strtolower($_SESSION['user_id']);

        // 添加代码查看链接
        $view_status[$i][7] = "";
        if ( $isAllowSeeCode ) {
            $view_status[$i][7] = "<a target=_blank href=showsource.php?id=".$row['solution_id'].">".$language_name[$row['language']]."</a>";
        }
        // 添加代码编辑链接
        if ( $isAllowEditCode ) {
            $view_status[$i][7].= "<a target=_self href='submitpage.php?id=".$row['problem_id']."&sid=".$row['solution_id']."'>/Edit</a>";
        }

        if ( strlen($view_status[$i][7]) == 0 ) {
            $view_status[$i][7]=$language_name[$row['language']];
        }
        $view_status[$i][8]= $row['code_length']." B";
    }
    else {
        // 评测进行中
        $view_status[$i][5]="----";
        $view_status[$i][6]="----";
        $view_status[$i][7]="----";
        $view_status[$i][8]="----";
    }
    $view_status[$i][9]= $row['in_date'];
    $view_status[$i][10]= $row['judger'];
}

?>

<?php
/////////////////////////Template
if (isset($_GET['cid']))
	require("template/".$OJ_TEMPLATE."/conteststatus.php");
else
	require("template/".$OJ_TEMPLATE."/status.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');
?>

